#include "checkitems.h"
#include "ui_checkitems.h"
#include<QLineEdit>
#include<QDebug>
#include<QJsonArray>
#include<QJsonDocument>
#include<QTextCodec>

CheckItems::CheckItems(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CheckItems)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
    on_pushButton_2_clicked();
}

CheckItems::~CheckItems()
{
    delete ui;
}

void CheckItems::on_pushButton_2_clicked()
{
    QLineEdit *tmp_line;
    if(lineFirstFlag){
        tmp_line=new QLineEdit("");
    }else{tmp_line=new QLineEdit("新的项目");};
    QListWidgetItem *item=new QListWidgetItem(ui->listWidget,tmp_list.length());
    tmp_list.append(tmp_line);
    ui->listWidget->setItemWidget(item,tmp_line);
    ui->listWidget->setFocus();
    ui->listWidget->setCurrentItem(item);
    tmp_line->selectAll();
}

void CheckItems::on_pushButton_clicked()
{
    int row=ui->listWidget->currentRow();
    ui->listWidget->takeItem(row);
    tmp_list.removeAt(row);
}

QString CheckItems::getItems(){
    return on_pushButton_3_clicked();
}

QString CheckItems::on_pushButton_3_clicked()
{
    QJsonArray itemsJsonList;
    bool isAllEmpty=true;
    for(QLineEdit* i:tmp_list){
        QString tmp=i->text().trimmed();
        if(tmp!=""){
            isAllEmpty&=false;
            itemsJsonList.push_back(tmp);
        }
    }
    if(isAllEmpty){
        itemsJsonList.push_back("设备是否能够正常运行？");
    }
    close();
    QJsonDocument document;
    document.setArray(itemsJsonList);
    QByteArray byteData=document.toJson(QJsonDocument::Compact);
    QString items=QTextCodec::codecForName("UTF-8")->toUnicode(byteData);
    return items;
}
